/*
package com.ikaros.jdk8.example;

import java.util.*;
import java.util.stream.Collectors;

import com.ikaros.jdk8.domain.Article;

*/
/**
 * @author: 胡国军
 * @Date 2018-01-09 15:46
 *//*

public class StreamAPI {

    public static List<Article> getArticleList(){
        List<String> tags=new ArrayList<>();
        tags.add("Java");
        tags.add("java JPA");
        tags.add("java MyBatis");

        List<String> tags2=new ArrayList<>();
        tags2.add("Java");
        tags2.add("java JPA");
        tags2.add("java MyBatis");
        tags2.add("java scurity");
        List<Article> articleList=new ArrayList<>();
        Article article1=new Article("javaShiro","ikaros",tags);
        Article article2=new Article("javaSecurity","知世",tags);
        Article article3=new Article("javaJPA","木之本",tags2);
        Article article4=new Article("javaMyBatis","sakura",tags);
        Article article5=new Article("javaMyBatis","sakura",tags2);
        articleList.add(article1);
        articleList.add(article2);
        articleList.add(article3);
        articleList.add(article4);
        articleList.add(article5);

        return articleList;
    }

    public static void main(String[] args) {
        // TODO: 2018/1/9 for循环比stream快 建议数据多用stream
        */
/*Article tagJava = getTagJava(getArticleList());
        System.out.println(tagJava.getAuthor());
        Optional<Article> firstJavaArticle = getFirstJavaArticle(getArticleList());
        System.out.println(firstJavaArticle.get().getAuthor());

        List<Article> allJavaArticles = getAllJavaArticles(getArticleList());
        System.out.println(allJavaArticles.size());
        List<Article> allJava8Articles = getAllJava8Articles(getArticleList());
        System.out.println(allJava8Articles.size());

        long l = System.currentTimeMillis();
        Map<String, List<Article>> groupByAuthor = groupByAuthor(getArticleList());
        long e = System.currentTimeMillis();
        Map<String, List<Article>> groupByAuthorJava8 = groupByAuthorJava8(getArticleList());
        long f = System.currentTimeMillis();
        System.out.println(e-l);
        System.out.println(f-e);*//*


        */
/*long l = System.currentTimeMillis();
        Set<String> distinctTags = getDistinctTags(getArticleList());
        long e = System.currentTimeMillis();
        Set<String> distinctTagsJava8 = getDistinctTagsJava8(getArticleList());
        long f = System.currentTimeMillis();
        System.out.println(e-l);
        System.out.println(f-e);*//*

        List<String> a1=new ArrayList<>();
        getArticleList().forEach(a-> {
            a1.add(a.getAuthor());
        });
        System.out.println(a1.size());
        Arrays.asList( "a", "b", "d" ).forEach( e -> System.out.println( e ) );

    }
    public static Article getTagJava(List<Article> articleList){
        for (Article article : articleList) {
            if (article.getTags().contains("Java")) {
                return article;
            }
        }
        return null;
    }

    public static Optional<Article> getFirstJavaArticle(List<Article> articleList) {
        return articleList.stream()
                .filter(article -> article.getTags().contains("Java"))
                .findFirst();
    }

    public static List<Article> getAllJavaArticles(List<Article> articleList) {
        List<Article> result = new ArrayList<>();
        for (Article article : articleList) {
            if (article.getTags().contains("Java")) {
                result.add(article);
            }
        }
        return result;
    }

    public static List<Article> getAllJava8Articles(List<Article> articles) {
        //List list= Collections.EMPTY_LIST;
        return articles.stream()
                .filter(article -> article.getTags().contains("Java"))
                .collect(Collectors.toList());
    }

    public static Map<String, List<Article>> groupByAuthor(List<Article> articleList) {
        Map<String, List<Article>> result = new HashMap<>();
        for (Article article : articleList) {
            if (result.containsKey(article.getAuthor())) {
                result.get(article.getAuthor()).add(article);
            } else {
                ArrayList<Article> articles = new ArrayList<>();
                articles.add(article);
                result.put(article.getAuthor(), articles);
            }
        }
        return result;
    }

    public static Map<String, List<Article>> groupByAuthorJava8(List<Article> articles) {
        return articles.stream()
                .collect(Collectors.groupingBy(Article::getAuthor));
    }

    public static Set<String> getDistinctTags(List<Article> articles) {
        Set<String> result = new HashSet<>();
        for (Article article : articles) {
            result.addAll(article.getTags());
        }
        return result;
    }

    public static Set<String> getDistinctTagsJava8(List<Article> articles) {
        return articles.stream()
                .flatMap(article -> article.getTags().stream())
                .collect(Collectors.toSet());
    }
}
*/
